const Controller = require('../controllers/Controller.js').controller;
const app = getApp();
Page({
  data: {
    globalStype:'',
    phoneNumber:'',
    openId:'',
    hasMobile: false, //没有手机号
    wxuserInfo:''
  },

  onLoad(option) {
    this.setData({globalStype:app.globalData.globalStype})
    
  },
  goagree(){
    wx.navigateTo({
      url: "/pages/Login/agree"
    })
  },
  appletBdMobileLogin(){
    Controller.appletBdMobileLogin({
      mobile:this.data.phoneNumber,
      openId:app.globalData.openId,
      nick:this.data.wxuserInfo.nickName,
      head:this.data.wxuserInfo.avatarUrl
    }).then(result => {
      wx.setStorageSync('usertoken', result.data.sessionId)
      wx.showToast({
        title: '登录成功！',
        icon: 'success',
        duration: 2000
      })
      setTimeout(res => {
        wx.navigateBack({ //返回
          delta: 1
        })
      })
    });
  },
  bindgetuserinfoBtn(e) {
    console.log("111111",e)
    if(e.detail.userInfo){
      this.setData({
        hasMobile: true,
        wxuserInfo:e.detail.userInfo
      })
      wx.setStorageSync('wxuserInfo', e.detail.userInfo)
    }
  },
  // 手机授权
  bindgetphonenumber(e) {
    var phone=e.detail;
    wx.login({
      success: res => {
        Controller.appletGetPhone({
          sessionKey: app.globalData.sessionKey,
          iv:phone.iv,
          encryptedData:phone.encryptedData
        }).then(result => {
            this.setData({
              phoneNumber:result.data.phoneNumber,
            })
            wx.setStorageSync('phonenumber',result.data.phoneNumber)
            this.appletBdMobileLogin()
        });
        
      }
    });

  },

  cancel(){
    wx.navigateBack({ //返回
      delta: 1
    })
  },

})